package edu.isi.pedwork;

import java.io.File;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordsGene {
	private static WordsGene wg = new WordsGene();

	private Connection conn = null;
	private Statement stmt = null;
	private StringBuffer train = null;
	private Map<String, Integer> wordsMap;
	// private String[] tables = { "Art_Design" };
	private String[] tables = { "Art_Design", "Books", "Business", "Charity",
			"Entertainment", "Family", "Fashion", "Food_Drink", "Health",
			"Music", "News", "Science", "Technology", "Sports" };

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		wg.process();
	}

	public void process() {
		this.buildTrain();
	}

	private void buildTrain() {
		try {
			init_db(Configure.dbPath);
			wordsMap = new HashMap<String, Integer>();
			train = new StringBuffer("");

			int count = 0;
			PrintWriter pw = new PrintWriter(new File("keyWords/train.txt"));
			for (String tName : tables) {

				ResultSet rs = stmt
						.executeQuery("select t.Tweet from tweets t , users u , "
								+ "user_topic ut where t.UserId = u.UserId and u.UserName = ut.userName and ut.topic = \""
								+ tName + "\"");
				while (rs.next()) {
					count++;
					String output = "";
					if(tName.equals("Science") || tName.equals("Technology")){
						output = count + "\t" + "Scien_Tech" + "\t";
					}else{
						output = count + "\t" + tName + "\t";
					}
					sentenceStemming(rs.getString("Tweet").toLowerCase().trim());
					String str = train.toString();

					Pattern p = Pattern.compile("[ \t\n\f\r]+");
					Matcher m = p.matcher(str);
					str = m.replaceAll(" ");

					output += str.trim() + "\n";
					train = new StringBuffer();
					if (str.trim() != null && str.trim().length() > 10) {
						pw.print(output);
					}
				}

			}
			pw.close();

			pw = new PrintWriter(new File("keyWords/term.txt"));

			Set<Entry<String, Integer>> es = wordsMap.entrySet();
			StringBuffer ts = new StringBuffer();
			for (Entry<String, Integer> word : es) {
				if (word.getValue() > 1) {
					ts.append(word.getKey());
					ts.append("\n");
				}
			}

			pw.print(ts.toString());
			pw.close();

			close_db();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void init_db(String connect) throws Exception {
		conn = getConnection(connect);
		stmt = conn.createStatement();
	}

	public void close_db() throws SQLException {
		stmt.close();
		conn.close();
	}

	private String sentenceStemming(String in) {
		String sentence = "";
		in = in.replace(",", " ");
		in = in.replace(".", " ");
		in = in.replace(":", " ");
		in = in.replace("!", " ");
		in = in.replace("_", " ");
		in = in.replace("-", " ");
		in = in.replace("'", " ");
		in = in.replace("\"", " ");
		in = in.replace("/", " ");

		String str[] = in.split(" ");
		Iterator itr = null;
		for (int i = 0; i < str.length; i++) {
			String w = WordProcess.process(str[i]);

			if (w != null) {
				train.append(w);
				train.append(" ");
				if (wordsMap.containsKey(w)) {
					wordsMap.put(w, wordsMap.get(w) + 1);
				} else {
					wordsMap.put(w, 1);
				}
			}
			/*
			 * if (wordsMap.containsKey(w)) { wordsMap.put(w, wordsMap.get(w) +
			 * 1); } else { wordsMap.put(w, 1); }
			 */
		}

		return sentence;
	}

	public Connection getConnection(String url) throws Exception {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		return DriverManager.getConnection(url);
	}
}
